$(document).ready(function(){

	/*
	 * This event will be fired when user click in button 
	 * to submit the form to register a new user
	 */
	$("#formNewUser").submit(function(event){

		event.preventDefault();//to wait in the same page

		if ($("#password").val() != $("#confirmPassword").val()) {
			alert("Passowrd does not match confirm Password");
			return;
		}
		var jsonData = {
			entry: {
				name		: $("#name").val(),
				username	: $("#username").val(),
				email		: $("#email").val(),
				password	: $("#password").val(),
				country		: $("#country").val(),
				accountType	: $("#accountType").val(),
				action		: $("#action").val()
			}

		};

		$.ajax({
			crossDomain: true,
			cache: false,
			contentyType: "application/json",
			dataType: "json",
			data: JSON.stringify(jsonData),
			type: "POST",
			url: "User",
			success: function (data, textStatus, jqXHR) {
	 			document.getElementById("message-success").innerHTML = data.message;
	 			document.getElementById("message-success").style.display = "block";
		 	},
		 	error: function (jqXHR, testStatus, errorThrown) {
		 		console.log("Error " + errorThrown.status);
		 		console.log("Error Message Log: " + errorThrown.statusText);
		 		alert("Error! Please, contact the administrator... " + errorThrown);
		 	}
		});
		
	});



	/**
	 * Load Countries in ComboBox
	 *
	 */
	$.ajax({
		crossDomain: true,
		cache: false,
		contentyType: "application/json",
		dataType: "json",
		type: "GET",
		url: "Country?action=getAllCountries",
		success: function (data, textStatus, jqXHR) {
			for (var i = 0; i < data.countries.length; i++) {
				document.getElementById("country").innerHTML += "<option value=\'" + data.countries[i].id + "'\">" + data.countries[i].name + "</option>";
			}
		},
		error: function (jqXHR, testStatus, errorThrown) {
			console.log("Error " + errorThrown.status);
			console.log("Error Message Log: " + errorThrown.statusText);
		}
	});
		

	
	
	/**
	 * Load UserTypes in ComboBox
	 *
	 */
	$.ajax({
		crossDomain: true,
		cache: false,
		contentyType: "application/json",
		dataType: "json",
		type: "GET",
		url: "UserType?action=getAllUserTypes",
		success: function (data, textStatus, jqXHR) {
			for (var i = 0; i < data.userTypes.length; i++) {
				document.getElementById("accountType").innerHTML += "<option value=\'" + data.userTypes[i].id + "'\">" + data.userTypes[i].description + "</option>";
			}
		},
		error: function (jqXHR, testStatus, errorThrown) {
			console.log("Error " + errorThrown.status);
			console.log("Error Message Log: " + errorThrown.statusText);
		}
	});




	/**
	 * OnChange username to validaty as unique username
	 *
	 */
	 $("#username").change(function() {
		 
	 	$.ajax({
			crossDomain: true,
			cache: false,
			contentyType: "application/json",
			dataType: "json",
			type: "GET",
			url: "User?action=validateUsername&username=" + $("#username").val(),
			beforeSend: function(){
//	 			document.getElementById("box-login").style.color = "#FFF";
//	 			document.getElementById("box-login").innerHTML = "<img src=\"css/img/ajax-loader.gif\"> Waiting...";
//	 			document.getElementById("box-login").style.display = "block";
	 			
	 		},
			success: function (data, textStatus, jqXHR) {
				if (data.isValid)
					alert("valid");
				else {
					alert("not valid");
					document.getElementById("username").value = "";
				}
			},
			error: function (jqXHR, testStatus, errorThrown) {
				console.log("Error " + errorThrown.status);
				console.log("Error Message Log: " + errorThrown.statusText);
			}
	 	});

	 });

});